package alpha.com.google.code.mechanic.janitor.controller;

import java.io.File;
import java.io.IOException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import alpha.com.google.code.mechanic.janitor.model.ContextsCollector;
import alpha.com.google.code.mechanic.janitor.model.DefaultSysFilePath;
import alpha.com.google.code.mechanic.janitor.model.ProjectContext;

/**
 * The class is to read the Context data stored in the context.con file under
 * the default directory.
 * 

 * @History
 * 1. change the FILE_NAME from private to public
 * @author jingjiejiang
 * @Edited: Oct 7, 2014
 * 
 * 2. Add a new method void saveContextsInfo ().
 * The method is to save the Contexts information to the context.con file.
 * @author jingjiejiang
 * @Edited Nov 3, 2014
 * 
 * @Last_edited: Nov 3, 2014
 * 
 * @author jingjiejiang
 * @since Aug 4, 2014
 *
 */
public class ContextData {
  
  // the default file that stores the user defined contexts info.
  public static final String FILE_NAME = "contexts.con";
  
  private DefaultSysFilePath sysDefFilePath = new DefaultSysFilePath();
  // store the user defined contexts
  private ContextsCollector contextsCollector = new ContextsCollector();
  private File contextsFile;

  public ContextData() {
    
  }
  
  public ContextsCollector getContextsCollector() {
    return contextsCollector;
  }

  public void setContextsCollector(final ContextsCollector contextsCollector) {
    this.contextsCollector = contextsCollector;
  }
  
  public File getContextsFile() {
    return contextsFile;
  }
  
  public String initCtxInfoFilePath() {
    
    String path = "";
    
    path = sysDefFilePath.getDefaultSysFilePath()
        + FILE_NAME;
    
    return path;
  }
     
  /**
   * Read previous defined contexts information from contexts.con file.
   *
   * @return
   * @throws IOException 
   */
  public boolean readContextsFile() throws Exception {
     
    File file = new File(initCtxInfoFilePath());
    
    contextsFile = file;
    if (file.exists()) {
      //contextsCollector.deserializeContextsInfo(contextsFile, projContext);
      try {
        initContextsCollector();
      } catch (Exception error) {
        throw error;
      }
      
      return true;
    }
    else {
      
      Shell parentShell = Display.getCurrent().getActiveShell();
      MessageDialog.openWarning(parentShell, "No contexts files", "You haven't"
          + " create your contexts file yet.");
      return false;
    }
    
  }
  
  /**
   * Initialise the contextsCollector with the data stored in the
   * context.con file.
   * @throws Exception an exception will be thrown when reading Contexts
   *         data from context.con file
   *
   */
  private void initContextsCollector() throws Exception {
    
    int size = 0;
    
    try {
      size = contextsCollector.getSavedCollectorSize(contextsFile);  
    } catch (IOException e) {
      throw e;
    }
    
    for (int count = 1; count <= size; count ++) {
      
      ProjectContext context = new ProjectContext();
      try {
        context = (ProjectContext) contextsCollector.deserializeContextInfo(
            contextsFile, count, context);
      } catch (Exception e) {
        throw e;
      }
      contextsCollector.addContext(context);
    }
  }
  
  public void saveContextsInfo () throws IOException {
    
    contextsCollector.serializeContextsInfo(contextsFile);
  }

}
